package com.cisdi.blog.web.admin;

import com.cisdi.blog.entity.Type;
import com.cisdi.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * 分类
 * @author pengyangyan
 * @version 1.0.0
 * @date 2020年03月19日 11:04:00
 */
@Controller
@RequestMapping("/admin/types")
public class TypeController {

    @Autowired
    TypeService typeService;

    @GetMapping
    public String index(@PageableDefault(size = 10,sort = {"id"},direction = Sort.Direction.DESC)Pageable pageable,
                        Model model){
        model.addAttribute("page",typeService.listType(pageable));
        return "admin/types";
    }

    @GetMapping("input")
    public String addType(Model model){
        model.addAttribute("type",new Type());
        return "admin/types_add";
    }

    @PostMapping("post")
    public String addType(Type type,BindingResult result, RedirectAttributes attributes){
        Type typeByName = typeService.findTypeByName(type.getName());
        if (typeByName!=null){
            result.rejectValue("name","nameError","类型名称已存在,请换一个");
        }
        if (result.hasErrors()){
            return "admin/types_add";
        }
        Type t = typeService.saveType(type);
        if (t == null){
            attributes.addFlashAttribute("message","新增失败");
            attributes.addFlashAttribute("status",false);
        }else {
            attributes.addFlashAttribute("message","新增成功");
            attributes.addFlashAttribute("status",true);
        }
        return "redirect:/admin/types";
    }

    @GetMapping("/{id}/input")
    public String updateType(@PathVariable("id") Long id,Model model){
        Type type = typeService.getType(id);
        model.addAttribute("type",type);
        return "admin/types_update";
    }

    @PostMapping("/post/{id}")
    public String updateType(@Valid Type type, BindingResult result, @PathVariable Long id,
                             Model model,RedirectAttributes attributes){
        Type typeByName = typeService.findTypeByName(type.getName());
        if (typeByName!=null){
            result.rejectValue("name","nameError","类型名称已存在,请换一个");
        }
        if (result.hasErrors()){
            model.addAttribute("type",type);
            return "admin/types-update";
        }
        Type t = typeService.updateType(id, type);
        if (t == null){
            attributes.addFlashAttribute("message","更新失败");
            attributes.addFlashAttribute("status",false);
        }else {
            attributes.addFlashAttribute("message","更新成功");
            attributes.addFlashAttribute("status",true);
        }
        return "redirect:/admin/types";
    }

    @GetMapping("/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        typeService.removeType(id);
        attributes.addFlashAttribute("message","删除成功");
        attributes.addFlashAttribute("status",true);
        return "redirect:/admin/types";
    }
}
